﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;


//Author: ljj
//Date: 20190624
//Function: buff类的基类
public class BaseBuff : MonoBehaviour
{
    public float effect;
    public float duration;
    protected bool isAwake = false;
    // Update is called once per frame
    void Update()
    {
        if (duration == -1)
            return;
        duration -= Time.deltaTime;
        Continued(Time.deltaTime);
        if (duration <= 0)
        {
            Destroy(this);
        }
    }

    public virtual void Continued(float delta)
    {

    }

    public virtual void Cover(float effect, float duration)
    {
        //Debug.Log("cover");
        if (isAwake)
            BuffDown();
        if (effect > this.effect)
        {
            this.effect = effect;
            this.duration = duration;
        }
        else
        {
            if (effect == this.effect)
            {
                if (duration == -1)
                    this.duration = -1;
                else
                    this.duration = Mathf.Max(this.duration, duration);
            }
        }

        if (isAwake)
            BuffUp();
    }

    public virtual void BuffUp() { }
    public virtual void BuffDown() { }

    public void AwakeNow()
    {
        if (!isAwake)
        {
            isAwake = true;
            BuffUp();
        }
    }

    void Start()
    {
        if (!isAwake)
        {
            isAwake = true;
            BuffUp();
        }
    }

    void OnDestroy()
    {
        if (isAwake)
        {
            BuffDown();
        }

    }
}
